<script>
	import {
		billing
	} from '@/api/mall.js';
	import {
		invoiceOrRedRush
	} from '@/api/ws.js';
	export default {
		name: "invoice",
		data() {
			return {
				orderId: null,
				formData: {
					CompanyName: '',
					CompanyTitle: '',
				},
				// 表单验证规则
				rules: {
					CompanyName: [{
						required: true,
						message: '请输入公司名称'
					}],
					CompanyTitle: [{
						required: true,
						message: '请输入纳税人识别号'
					}],
				
				},
				// 提交按钮状态
				isSubmitting: false,
				isShip: false,
				tipsData:null
			}
		},
		onLoad(options) {
			if (options.id) {
				this.orderId = options.id;
			}
			if (options.isShip) {
				this.isShip = options.isShip
			}
			let invoiceTicketList = uni.getStorageSync('invoiceTicketList')
			let invoiceList = []
			let noInvoiceList = []
			let price = 0;
			invoiceTicketList.map(v => {
				if (v.invoiceStatus == 'N') {
					noInvoiceList.push(v)
				} else {
					// 金额转为分进行累加，避免精度问题，显式转换为数字
					price += Math.round((Number(v.invoiceAmount) || 0) * 100)
					invoiceList.push(v)
				}
			})
			// 累加后转回元，保留两位小数
			this.tipsData = {
				invoiceList,
				noInvoiceList,
				price: (price / 100).toFixed(2)
			}
			console.log("tipsData =>",this.tipsData);
			if (noInvoiceList.length > 0) {
				this.$nextTick(() => {
					this.$refs.tipsPopup.open();
				})
			}
			uni.setNavigationBarTitle({
				title: '申请开票'
			});
		},
		methods: {
			closeTips() {
				this.$refs.tipsPopup.close();
			},
			openTips() {
				this.$refs.tipsPopup.open();
			},
			// 表单验证
			validateForm() {
				let isValid = true;

				// 检查公司名称
				if (!this.formData.CompanyName.trim()) {
					uni.showToast({
						title: '请输入公司名称',
						icon: 'none'
					});
					return false;
				}

				// 检查发票抬头
				if (!this.formData.CompanyTitle.trim()) {
					uni.showToast({
						title: '请输入纳税人识别号',
						icon: 'none'
					});
					return false;
				}
				return isValid;
			},

			// 提交表单
			async submitForm() {
				if (this.isSubmitting) return;

				// 表单验证
				if (!this.validateForm()) return;

				try {
					this.isSubmitting = true;
					uni.showLoading({
						title: '提交中...'
					});
					let response = null
					if (this.isShip) {
						// 构建请求参数
						const params = {
							invoiceType: 1,
							ghfmc: this.formData.CompanyName,
							ghfnsrsbh: this.formData.CompanyTitle,
							orderNo: this.orderId,
							tickets:this.tipsData.invoiceList
						};
						// 调用接口
						response = await invoiceOrRedRush(params);
					} else {
						// 构建请求参数
						const params = {
							OrderId: this.orderId,
							...this.formData
						};
						// 调用接口
						response = await billing(params);
					}



					uni.hideLoading();
					this.isSubmitting = false;


					uni.showToast({
						title: '开票申请成功',
						icon: 'success',
						duration: 2000
					});

					// 延迟返回上一页
					setTimeout(() => {
						uni.navigateBack();
					}, 1000);

				} catch (error) {
					console.error('开票申请失败:', error);
					uni.hideLoading();
					this.isSubmitting = false;

					uni.showToast({
						title: '开票申请失败，请稍后重试',
						icon: 'none'
					});
				}
			}
		}
	}
</script>

<template>
	<view class="invoice-container">
		<!-- 表单区域 -->
		<view class="form-card">
			<view class="form-title">开票信息</view>
		
			<view class="form-item">
				<text class="form-label">公司名称<text class="required">*</text></text>
				<input class="form-input" type="text" v-model="formData.CompanyName" placeholder="请输入公司名称" />
			</view>

			<view class="form-item">
				<text class="form-label">纳税人识别号<text class="required">*</text></text>
				<input class="form-input" type="text" v-model="formData.CompanyTitle" placeholder="请输入纳税人识别号" />
			</view>

			
		</view>

		<view class="tips-section">
			<view class="tips-title">开票须知：</view>
			<view class="tips-item">1. 发票内容将根据订单商品信息开具，不可修改。</view>
			<view class="tips-item">2. 发票将在申请成功后5个工作日内发送至您填写的邮箱。</view>
			<view class="tips-item">3. 每个订单只能申请一次开票，请确保信息填写无误。</view>
			<view class="tips-item">4. 如有疑问，请联系客服。</view>
		</view>

		<!-- 提交按钮 -->
		<button class="submit-button" :disabled="isSubmitting" @click="submitForm">提交申请</button>
		<uni-popup ref="tipsPopup" type="center">
			<view class="tipsBox" v-if="tipsData">
				<view class="title">
					温馨提示
				</view>
				<view class="content">
					<view class="item">
						可开票金额：{{tipsData.price}}
					</view>
					<view class="item" v-if="tipsData.invoiceList && tipsData.invoiceList.length>0">
						<view class="itemTitle">
							可开发票订单
						</view>
						<view class="iItem" v-for="(item,index) in tipsData.invoiceList" :key="index">
							{{index + 1}}、{{item.passengerName}}：{{item.startPortName}}->{{item.arrivePortName}}
						</view>
					</view>
					<view class="item" v-if="tipsData.noInvoiceList && tipsData.noInvoiceList.length>0">
						<view class="itemTitle">
							不可开发票订单
						</view>
						<view class="iItem" v-for="(item,index) in tipsData.noInvoiceList" :key="index">
							{{index + 1}}、{{item.passengerName}}：{{item.startPortName}}->{{item.arrivePortName}}
						</view>
					</view>
				</view>
				<view class="btnList">
					<view class="singleBtn" @click="closeTips">
						关闭
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<style scoped lang="scss">
	.tipsBox {
		background-color: #fff;
		width: 90vw;
		border-radius: 20rpx;
		text-align: left;

		.title {
			font-size: 36rpx;
			font-weight: 600;
			padding: 40rpx;
			text-align: center;
		}

		.content {
			padding: 0 30rpx 40rpx 30rpx;
			width: 100%;
			box-sizing: border-box;

			.item {
				font-size: 30rpx;
				padding-bottom: 10rpx;
			}

			.tipsText {
				padding: 20rpx 0;
			}
		}

		.btnList {
			display: flex;
			align-items: center;
			width: 100%;
			border-top: 2rpx solid #999;

			.singleBtn {
				width: 100%;
				height: 100rpx;
				line-height: 100rpx;
				text-align: center;
				color: #2796ff;
			}

			.btn {
				width: 50%;
				height: 100rpx;
				line-height: 100rpx;
				text-align: center;

				&.define {
					border-left: 2rpx solid #999;
					color: #2796ff;
				}
			}
		}
	}

	.invoice-container {
		min-height: 100vh;
		background-color: #F5F6F7;
		padding: 30rpx;
	}

	.form-card {
		background-color: #fff;
		border-radius: 16rpx;
		padding: 30rpx;
		margin-bottom: 30rpx;
		box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);

		.form-title {
			font-size: 34rpx;
			font-weight: bold;
			color: #333;
			margin-bottom: 30rpx;
			padding-bottom: 20rpx;
			border-bottom: 1rpx solid #f0f0f0;
		}

		.form-item {
			margin-bottom: 30rpx;

			.form-label {
				display: block;
				font-size: 28rpx;
				color: #333;
				margin-bottom: 16rpx;

				.required {
					color: #FF5151;
					margin-left: 6rpx;
				}
			}

			.form-input {
				width: 100%;
				height: 90rpx;
				background-color: #F8F8F8;
				border-radius: 12rpx;
				padding: 0 20rpx;
				font-size: 28rpx;
				color: #333;
				box-sizing: border-box;
				border: 1rpx solid #EFEFEF;
			}
		}
	}

	.tips-section {
		background-color: #fff;
		border-radius: 16rpx;
		padding: 30rpx;
		margin-bottom: 40rpx;
		box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);

		.tips-title {
			font-size: 28rpx;
			font-weight: bold;
			color: #333;
			margin-bottom: 20rpx;
		}

		.tips-item {
			font-size: 26rpx;
			color: #666;
			margin-bottom: 10rpx;
			line-height: 1.5;
		}
	}

	.submit-button {
		width: 100%;
		height: 90rpx;
		line-height: 90rpx;
		background: linear-gradient(135deg, #498FFF 0%, #3A7FE6 100%);
		color: #fff;
		font-size: 32rpx;
		font-weight: 500;
		text-align: center;
		border-radius: 45rpx;
		margin-top: 60rpx;

		&:active {
			opacity: 0.9;
		}

		&[disabled] {
			background: #CCCCCC;
			color: #fff;
		}
	}
</style>